<template>
  <div class="m-view-awaitVerifyList m-window">
    <div class="m-condition">
      <el-form :model="searchForm">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="工厂代码：" label-width="88px">
              <el-input
                v-model="searchForm.factory"
                placeholder="输入工厂代码"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="零件类型：" label-width="88px">
              <el-input
                v-model="searchForm.partType"
                placeholder="输入零件类型"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料描述（材质）：" label-width="140px">
              <el-input
                v-model="searchForm.matDescription"
                placeholder="输入物料描述"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建日期：" label-width="88px">
              <el-date-picker
                v-model="searchForm.createDate"
                type="date"
                placeholder="输入日期"
                value-format="yyyy-MM-dd"
                :editable="false"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="申购单号：" label-width="88px">
              <el-input
                v-model="searchForm.purchaseNum"
                placeholder="输入单号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="模具编号：" label-width="88px">
              <el-input
                v-model="searchForm.mouldNum"
                placeholder="请输入模具编号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="审核状态：" label-width="88px">
              <el-select
                placeholder="审核状态"
                v-model="searchForm.state"
                clearable
                  @change="changeState"
              >
                <el-option
                  v-for="(item, index) in statusItem"
                  :key="index"
                  :label="item.name"
                  :value="item.code"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="2" class="m-floatright">
            <el-button type="primary" @click="verify(3)">退回</el-button>
          </el-col>

          <el-col :span="2" class="m-floatright">
            <el-button type="primary" @click="verify(2)">审核</el-button>
          </el-col>
          <el-col :span="2" class="m-floatright">
            <el-button type="primary" @click="getPurchaseRequest"
              >查询</el-button
            >
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="m-agTable m-bottom m-applySoleListTable">
      <ag-grid-vue
        class="m-agTableContent ag-theme-balham m-agCenter"
        :gridOptions="gridOptions"
        :columnDefs="columnDefs"
        :rowData="rowData"
        v-loading="loading"
      ></ag-grid-vue>
    </div>
      <div class="m-page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handlePrevClick"
        @next-click="handleNextClick"
        :current-page="searchForm.current"
        :page-sizes="[30, 50, 100, 150, 200]"
        :page-size="searchForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="searchForm.total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import gridOptions from "lesso-common/public/gridOptions";
import _Api from "@/api";
import authorize from "@/public/authorize";
import constant from "@/public/constant";
export default {
  data() {
    return {
      searchForm: {
        factory: "", //工厂代码
        partType: "", //零件类型
        matDescription: "", //材质
        createDate: "", //日期
        purchaseNum: "", //单号
        mouldNum: "", //模具编号
        state: null, //审核状态
        // auditor: null, //审核人
        size: 30,
        current: 1,
         total:0,
      },
      // 权限控制
      // todo : 对接权限后需要把btnStatus全部置为false
      pageCode: "verifySoleList",
      btnStatus: {
        query: false, //查询
        verify: false, //审核
        goBack: false, //退回
        toPurchase: false, //转采购申请
      },
      awaitVerify: false,

      statusItem: [
        { code: 1, name: "待审" },
        { code: 2, name: "通过" },
        { code: 3, name: "退回" },
      ], //状态列表

      //ag-grid 配置项
      gridOptions: null,
      columnDefs: null,
      rowData: [], //  表格数据
      loading: false, //加载条
    };
  },
  beforeMount() {
   
  },
  props: {
    addTab: {
      type: Function,
    },
  },
  created(){
 // 审核
    this.gridOptions = gridOptions();
    this.columnDefs = [
      {
        headerName: "申购单号",
        field: "purchaseNum",
        checkboxSelection: true,
        headerCheckboxSelection: true,
        headerCheckboxSelectionFilteredOnly: true,
         width: 160 
      },
      { headerName: "序号", field: "sequence", width: 70 },
      { headerName: "模具编号", field: "mouldNum",width: 90 },
      { headerName: "SAP名称", field: "sapName",width: 90 },
      { headerName: "零件类型", field: "partType",width: 90 },
      { headerName: "零件名称", field: "partName" ,width: 90},
      { headerName: "材质", field: "matDescription" ,width: 68},
      { headerName: "物料编码", field: "matNum",width: 100 },
     
      { headerName: "长", field: "length" ,width: 52},
      { headerName: "长度公差", field: "lengthTolerance" ,width: 90,
      cellRenderer: (params) => {
          if (params.value.indexOf("，") != -1) {
            let numArr = params.value.split("，");
            return (
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[0] +
              "</div>" +
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[1] +
              "</div>"
            );
          } else if (params.value.indexOf(",") != -1) {
            let numArr = params.value.split(",");
            return (
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[0] +
              "</div>" +
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[1] +
              "</div>"
            );
          }
        },},
      { headerName: "宽", field: "width",width: 52 },
      { headerName: "宽度公差", field: "widthTolerance" ,width: 90,
      cellRenderer: (params) => {
          if (params.value.indexOf("，") != -1) {
            let numArr = params.value.split("，");
            return (
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[0] +
              "</div>" +
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[1] +
              "</div>"
            );
          } else if (params.value.indexOf(",") != -1) {
            let numArr = params.value.split(",");
            return (
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[0] +
              "</div>" +
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[1] +
              "</div>"
            );
          }
        },},
      { headerName: "高", field: "heigth",width: 52 },
      { headerName: "高度公差", field: "heigthTolerance",width: 90,
      cellRenderer: (params) => {
          if (params.value.indexOf("，") != -1) {
            let numArr = params.value.split("，");
            return (
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[0] +
              "</div>" +
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[1] +
              "</div>"
            );
          } else if (params.value.indexOf(",") != -1) {
            let numArr = params.value.split(",");
            return (
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[0] +
              "</div>" +
              "<div style='word-wrap:break-word;word-break: break-all !important;'>" +
              numArr[1] +
              "</div>"
            );
          }
        }, },
      { headerName: "数量", field: "amount",width: 68  },
        { headerName: "硬度", field: "hardness" ,width: 68},
      { headerName: "是否倒角", field: "chamfer",width: 90 },
      { headerName: "单重", field: "pieceWeight" ,width: 68 },
      { headerName: "总重", field: "grossWeight",width: 68  },
      { headerName: "设计", field: "designer",width: 68  },
      { headerName: "工厂代码", field: "factory" ,width: 90 },
      { headerName: "创建日期", field: "createDate" ,width: 90},
      { headerName: "审核人", field: "auditor" ,width: 85},
      { headerName: "审核时间", field: "auditDate" ,width: 90},
      {
        headerName: "审核状态",
        field: "state",
        width: 90,
        valueFormatter: (params) => {
          for (let i = 0; i < this.statusItem.length; i++) {
            const status = this.statusItem[i];
            if (status.code === params.value) {
              return status.name;
            }
          }
          return params.value;
        },
      },
    ];
      this.gridOptions.defaultColDef.autoHeight = true;
    this.gridOptions.defaultColDef.cellClass = "cell-wrap-text";

  },
  mounted() {
    // 请求权限
    authorize.getBtnAuth(this.pageCode, this.btnStatus).then((status) => {
      console.log(status, "按钮权限");
    });
  
    this.searchForm.createDate = this.$utils.dateFormat('yyyy-MM-dd', new Date());   //设置默认日期今日
    this.getPurchaseRequest();
  },
  methods: {
    // 查询列表
    getPurchaseRequest() {
      
      let params = {
        size: this.searchForm.size,
        current: this.searchForm.current,
         leParams:{
          state:3
        },
        geParams:{
           state:1
        },
        eqParams: {
          state: this.searchForm.state,
        }, //精确查询
        likeParams: {
          factory: this.searchForm.factory,
          partType: this.searchForm.partType,
          createDate: this.searchForm.createDate,
          matDescription: this.searchForm.matDescription,
          mouldNum: this.searchForm.mouldNum,

          purchaseNum: this.searchForm.purchaseNum,
        }, //模糊查询
        orderBy: "createDate",
        isAsc: "false",
      };
      this.loading = true;
      _Api
        .getPurchaseRequestList(params)
        .then((response) => {
          let data = response.data;
             this.searchForm.total = data.total;
          if (data.code == 1 && data.total != 0) {
            this.rowData = data.rows;
            this.loading = false;
          } else {
            this.loading = false;
            this.rowData = [];
          }
        })
        .catch((err) => {
          this.rowData = [];
          this.loading = false;
          this.$alert(err.data.message, {
            showClose: false,
            confirmButton: "确定",
            customClass: "alertFailure",
          });
        });
    },
    //   审核 2/退回 3
    verify(stateData) {
      let rows = this.gridOptions.api.getSelectedRows();
     
      if (rows.length <= 0) {
        this.$alert("提示：请选择需要审核的数据", {
          showClose: false,
          confirmButtonText: "确定",
          customClass: "alertPrompt",
        });
        return;
      }
      let updateRows = [];
      rows.forEach((item) => {
        let obj = {};
        obj = item;

         obj.modifyer=constant.user.loginName;//登录人名称
        obj.modifyerId=constant.user.id;//登录人Id
        obj.modifyDate=this.curentTime();//获取当前时间

        obj.state = stateData;
        updateRows.push(obj);
      });
    
      _Api
        .submiteviewer(updateRows)
        .then((response) => {
          let data = response.data;
          if (data.code == 1) {
            this.$message({
              message: data.msg,
              type: "success",
            });

            this.getPurchaseRequest();
            // if(stateData==3){
            //    this.addTab({
            //     cName: "申购单申请列表",
            //     name: "applySoleList",
            //     completed: false,
            //   });
            // };
            //   if(stateData==2){
            //    this.addTab({
            //     cName: "已审核列表",
            //     name: "verifySoleList",
            //     completed: false,
            //   });
            // };

          } else {
            this.$message({
              message: data.msg,
              type: "warning",
            });
          }
        })
        .catch((err) => {
          this.$alert(err.data.message, {
            showClose: false,
            confirmButton: "确定",
            customClass: "alertFailure",
          });
        });
    },
     // 获取当前日期时间
    curentTime() {
      var now = new Date();

      var year = now.getFullYear(); //年
      var month = now.getMonth() + 1; //月
      var day = now.getDate(); //日

      var hh = now.getHours(); //时
      var mm = now.getMinutes(); //分
      var ss = now.getSeconds(); //分

      var clock = year + "-";

      if (month < 10) clock += "0";

      clock += month + "-";

      if (day < 10) clock += "0";

      clock += day + " ";

      if (hh < 10) clock += "0";

      clock += hh + ":";
      if (mm < 10) clock += "0";
      clock += mm + ":";

      if (ss < 10) clock += "0";
      clock += ss;
      return clock;
    },
        // 每页显示条数改变回调
    handleSizeChange(val) {
      this.searchForm.size = val;
      this.getPurchaseRequest();
    },
    // 页码改变回调
    handleCurrentChange(val) {
      this.searchForm.current = val;
      this.getPurchaseRequest();
    },
    // 点击上一页回调
    handlePrevClick(val) {
      this.searchForm.current = val;
    },
    // 点击下一页回调
    handleNextClick(val) {
      this.searchForm.current = val;
    },
    // 改变审核状态
    changeState(data) {
      this.searchForm.state = data;
      this.getPurchaseRequest();
    },
  },
};
</script>
<style lang="less">
.cell-wrap-text {
  white-space: normal !important;
}
</style>